<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/22
 * Time: 11:17
 */

namespace common\business\file;


use common\business\Handler;
use common\business\task\TaskResponseData;
use common\consts\CommonResources;
use common\models\File;
use yii\helpers\Json;

abstract class FileHandler extends Handler
{
    protected $file_id;                     // 文件id
    protected $filename;                    // 文件名

    protected $start_row;                   // 开始读取行号

    protected $rows_total;                  // 文件总行数
    protected $rows_insert;                 // 插入总条数
    protected $rows_cover;                  // 覆盖总条数
    protected $rows_skip;                   // 跳过总条数
    protected $rows_error;                  // 失败总条数

//    /**
//     * @var array
//     */
//    protected $errors = [];      // 错误

    /**
     * @var File
     */
    protected $file;

    public function __construct($file_id)
    {
        $this->file_id = $file_id;
    }


    /**
     * @param callback|null $writeBackHandler
     * @return TaskResponseData
     */
    public function execute($writeBackHandler = null){
        if (!$this->preProcess()) {
            $errMsg = CommonResources::ACTION_FAIL_MSG('Pre Process', 'File', Json::encode($this->getErrors()));
            return new TaskResponseData(TaskResponseData::CODE_ERROR, $errMsg);
        }
        if (!$this->process($writeBackHandler)) {
            $errMsg = CommonResources::ACTION_FAIL_MSG('Process', 'File', Json::encode($this->getErrors()));
            return new TaskResponseData(TaskResponseData::CODE_ERROR, $errMsg);
        }
        if (!$this->afterProcess()) {
            $errMsg = CommonResources::ACTION_FAIL_MSG('After Process', 'File', Json::encode($this->getErrors()));
            return new TaskResponseData(TaskResponseData::CODE_ERROR, $errMsg);
        }

        return new TaskResponseData(TaskResponseData::CODE_SUCCESS, CommonResources::ACTION_NAME_MODEL_SUCCESS('Execute', 'File', $this->file->file_name));
    }

    /**
     * 预处理
     *
     * @param $file_id string
     * @return boolean
     */
    protected function preProcess()
    {
        $this->file = File::findOne($this->file_id);

        if(!$this->file){
            $this->addError('Pre Process', CommonResources::ACTION_NAME_MODEL_FAIL('Find','File', $this->file_id, 'File ID not found in DB.'));
            return false;
        }

//        $fileName4check = iconv('utf-8', 'GBK//TRANSLIT//IGNORE', $this->file->file_name);
//        $fileName4check = iconv('gb2312', 'utf-8', $this->file->file_name);
//        $this->filename = $this->file->file_path.$fileName4check;
        $this->filename = $this->file->file_fullname;

        $this->rows_total = $this->file->file_rows;
        return true;
    }

    /**
     * 处理文件
     * @param callback|null $writeBackHandler
     * @return boolean
     */
    abstract protected  function process($writeBackHandler = null);

    /**
     * 后处理文件
     *
     * @return boolean
     */
    protected function afterProcess()
    {
        $this->file->file_rows = $this->rows_total;
        $this->file->file_rows_insert = $this->rows_insert;
        $this->file->file_rows_error = $this->rows_error;
        $this->file->file_rows_update = $this->rows_cover;
        $this->file->file_rows_skip = $this->rows_skip;
        if (!$this->file->save()){
            $dbErrorMsg = Json::encode($this->file->getErrors());
            $this->addError('Save File', $dbErrorMsg);
            return false;
        }
        return true;
    }
}